package dao

import (
	"zy35/inits"
	"zy35/model"
)

// 文章发布
func CreateBook(title string, context string) (book *model.Book, err error) {
	book = &model.Book{
		Title:   title,
		Context: context,
	}
	err = inits.DB.Create(&book).Error
	if err != nil {
		return nil, err
	}
	return book, nil
}

// 文章点赞
func BookLike(bookId int) *model.BookLike {
	result := &model.BookLike{
		BookId: bookId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

// 添加评论
func AddReview(bookId int, review string) *model.BookReview {
	result := &model.BookReview{
		BookId: bookId,
		Review: review,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

// 回复评论
func ResReview(bookId int, review string, fatherId int) *model.BookReview {
	result := &model.BookReview{
		BookId:   bookId,
		FatherId: fatherId,
		Review:   review,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

// 评论点赞
func AddReviewLike(reviewId int) *model.ReviewLike {
	result := &model.ReviewLike{
		ReviewId: reviewId,
	}
	err := inits.DB.Create(&result).Error
	if err != nil {
		return nil
	}
	return result
}

func GetBookId(id int) (book *model.Book) {
	err := inits.DB.Where("id=?", id).Where("deleted=?", 0).Find(&book).Error
	if err != nil {
		return nil
	}
	return book
}

func CreateLikeNum(bookId int) int {
	var count int64
	err := inits.DB.Model(&model.BookReview{}).Where("book_id=?", bookId).Count(&count).Error
	if err != nil {
		return 0
	}
	return int(count)
}
